/**
 * Created by DukeCrushIt on 2015/8/13.
 */
//module game{
    class BuyPetPanel extends eui.Component{
        public selectedGender:number;
        public maleNames:string[];
        public femaleNames:string[];
        public curPet:GoodInfo;
        public constructor(){
            super();
            this.skinName = "pet.BuyPetSkin";
            this.addEventListener(egret.Event.ADDED_TO_STAGE,this.addedToStage, this );
        }

        public showPet(pet:GoodInfo){
            this.curPet = pet;
            if( this.maleNames == undefined){
                var names:string = RES.getRes("names_cvs");
                var tempArr:string[] = names.split(":");
                this.maleNames = tempArr[0].split(",");
                this.femaleNames = tempArr[1].split(",");
            }
            DisplayController.instance().addUiElement(GameConst.GrayMask);
            DisplayController.instance().addUiElement(BuyPetPanel.getInstance());
        }

        private onBuy(evt:egret.TouchEvent){
            window["__buy"](this.input_name.text,this.curPet.id,this.selectedGender);
            this.onClose(null);
        }
        private onClose(evt:egret.TouchEvent){
            DisplayController.instance().removeUiElement(GameConst.GrayMask);
            DisplayController.instance().removeUiElement(BuyPetPanel._instance);
        }
        public selectGender(gender:number){
            this.selectedGender = gender;
            this.img_male_selected.visible = this.img_female_normal.visible = gender == Model.MALE;
            this.img_male_normal.visible = this.img_female_selected.visible = gender == Model.FEMALE;
            this.onRandomName(null);
        }
        private onGenderSelect(evt:egret.TouchEvent){
            if( evt.target == this.img_male_normal || evt.target == this.img_male_selected ){
                this.selectGender(Model.MALE);
            }else if(  evt.target == this.img_female_normal || evt.target == this.img_female_selected ){
                this.selectGender(Model.FEMALE);
            }
        }
        private onRandomName(evt:egret.TouchEvent){
            var arr:string[] = this.selectedGender == 0 ? this.maleNames : this.femaleNames;
            var randomIndex:number = Math.round(arr.length * Math.random());
            this.input_name.text = arr[randomIndex];
        }
        private img_pop_bottom_1:eui.Image;
        private img_pop_bottom_2:eui.Image;
        private img_close:eui.Image;
        private img_name_bg:eui.Image;
        private input_name:eui.TextInput;
        private img_refresh:eui.Image;
        private img_female_normal:eui.Image;
        private img_female_selected:eui.Image;
        private img_male_normal:eui.Image;
        private img_male_selected:eui.Image;
        private img_buy:eui.Image;
        public partAdded(name:string, instance:any){
            if( name == "img_pop_bottom_1"){
                this.img_pop_bottom_1 = instance;
            }else if(name == "img_close"){
                this.img_close = instance;
                this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose,this);
            }else if(name == "img_name_bg"){
                this.img_name_bg = instance;
            }else if(name == "input_name"){
                this.input_name = instance;
            }else if(name == "img_refresh"){
                this.img_refresh = instance;
                this.img_refresh.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRandomName, this);
            }else if(name == "img_female_normal"){
                this.img_female_normal = instance;
                this.img_female_normal.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGenderSelect, this);
            }else if(name == "img_female_selected"){
                this.img_female_selected = instance;
                this.img_female_selected.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGenderSelect, this);
            }else if(name == "img_male_normal"){
                this.img_male_normal = instance;
                this.img_male_normal.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGenderSelect, this);
            }else if(name == "img_male_selected"){
                this.img_male_selected = instance;
                this.img_male_selected.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGenderSelect, this);
            }else if(name == "img_buy"){
                this.img_buy = instance;
                this.img_buy.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBuy,this);
            }
        }
        public measure(){
            super.measure();
            this.x = GameConst.StageW - this.measuredWidth >> 1;
            this.y = GameConst.StageH - this.measuredHeight >> 1;
        }
        private addedToStage(evt:egret.Event){
            var names:string = RES.getRes("names_cvs");
            var tempArr:string[] = names.split(":");
            this.maleNames = tempArr[0].split(",");
            this.femaleNames = tempArr[1].split(",");
            this.selectGender(0);
        }
        public childrenCreated(){
            super.childrenCreated();
            ButtonIconMgr.getInstance().addButtonIcon(this.img_buy);
        }
        private static _instance:BuyPetPanel;
        public static getInstance():BuyPetPanel{
            if( BuyPetPanel._instance == null)
                BuyPetPanel._instance = new BuyPetPanel();
            return BuyPetPanel._instance;
        }
    }
//}